package com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition.onlook;

import android.os.Bundle;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.basic.utils.Loog;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.BR;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.OnlookAreaAdp;
import com.baihan.angus.app.game.busshow.databinding.FragmentOnlookAreaBinding;
import com.baihan.angus.app.game.common.bean.response.RoomResponse;
import com.baihan.angus.app.game.common.bean.response.Userinfo;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.model.VoiceRoomManager;
import com.tencent.imsdk.v2.V2TIMGroupInfoResult;

import java.util.HashMap;
import java.util.Map;

/**
 * 围观区
 */
public class OnlookAreaFragment extends BaseFragment<FragmentOnlookAreaBinding, OnlookAreaVM> {

    private Map<String, Object> mParameter = new HashMap<>();

    private OnlookAreaAdp mOnlookAreaAdp;

    private Userinfo userInfo;
    private int mPage = 1;
    private int mSize = 10;
    private int mType = 1;
    private int totalPage = 0;

    public OnlookAreaFragment() {
        // Required empty public constructor
    }

    public static OnlookAreaFragment newInstance() {
        OnlookAreaFragment fragment = new OnlookAreaFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.onlookAreaVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_onlook_area;
    }

    @Override
    protected void initView() {

        userInfo = UserConfig.getUserConfig().getUser();

        mOnlookAreaAdp = new OnlookAreaAdp();
        mVDBinding.rvOnlook.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mOnlookAreaAdp.bindToRecycleView(mVDBinding.rvOnlook);

        mVDBinding.srlOnlook.setOnRefreshListener(refreshLayout -> {
            mPage = 1;
            mParameter.put("page", mPage);
            mViewModel.getOnlookAreaRoom(mParameter);
        });

        mVDBinding.srlOnlook.setOnLoadMoreListener(refreshLayout -> {
            mParameter.put("page", ++mPage);
            mViewModel.getOnlookAreaRoom(mParameter);
        });

        mOnlookAreaAdp.setOnItemClickListener(new OnlookAreaAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                enterRoom(mOnlookAreaAdp.getItem(position).getRoom_id());
            }
        });
    }

    @Override
    protected void initData() {
        mParameter.put("page", mPage);
        mParameter.put("size", mSize);
        mParameter.put("type", mType);
        mViewModel.getOnlookAreaRoom(mParameter);
    }

    @Override
    protected void initObservers() {
        mViewModel.mldOnlookRoom.observe(this, new Observer<RoomResponse.DataX>() {
            @Override
            public void onChanged(RoomResponse.DataX dataX) {
                if (mVDBinding.srlOnlook.isRefreshing()) mVDBinding.srlOnlook.finishRefresh();
                if (mVDBinding.srlOnlook.isLoading()) mVDBinding.srlOnlook.finishLoadMore();

                if (dataX == null) return;

                if (mPage == 1) {
                    mOnlookAreaAdp.setNewData(dataX.getData());
                } else {
                    mOnlookAreaAdp.addAll(dataX.getData());
                }

                if (dataX.getTotal() % mSize > 0) {
                    totalPage = dataX.getTotal() / mSize + 1;
                } else {
                    totalPage = dataX.getTotal() / mSize;
                }

                if (mPage < totalPage) {
                    mVDBinding.srlOnlook.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlOnlook.setEnableLoadMore(false);
                }
            }
        });
    }

    private void enterRoom(final String roomId) {
        VoiceRoomManager.getInstance().getGroupInfo(roomId, new VoiceRoomManager.GetGroupInfoCallback() {
            @Override
            public void onSuccess(V2TIMGroupInfoResult result) {
                if (isRoomExist(result)) {
                    realEnterRoom(roomId);
                } else {
                    Tooast.normalInfo(getString(R.string.roomNotExist));
                }
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.normalInfo(msg);
            }
        });
    }

    private void realEnterRoom(String roomId) {
        String userId = userInfo.getId();
//        ARouter.getInstance().build(ArouterAppPath.VOICE_ROOM_AUDIENCE_ACTIVITY)
//                .withString("roomId", roomIdStr)
//                .withString("userId", userId)
//                .withInt("audio_quality", TRTCCloudDef.TRTC_AUDIO_QUALITY_DEFAULT)
//                .navigation();
//        VoiceRoomAudienceActivity.enterRoom(getActivity(), roomData,roomId, userId, TRTCCloudDef.TRTC_AUDIO_QUALITY_DEFAULT);
    }

    private boolean isRoomExist(V2TIMGroupInfoResult result) {
        if (result == null) {
            Loog.e(getString(R.string.roomNotExist));
            return false;
        }
        return result.getResultCode() == 0;
    }
}